package com.cocostudio.erpapp.provider;

import com.cocostudio.erpapp.RegLoginActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public final class Settings {
    private static final String name = "settings";

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(name, 0);
    }

    public static void saveUID(Context context, String uid) {
        Editor editor = getSharedPreferences(context).edit();
        editor.putString("uid", uid);
        editor.commit();
    }

    public static String getUID(Context context) {
        return getSharedPreferences(context).getString("uid", null);
    }

    public static boolean isLogined(Context context) {
        return getSharedPreferences(context).getString("uid", null) != null;
    }

    public static void logout(Context context) {
        Editor editor = getSharedPreferences(context).edit();
        editor.remove("uid");
        editor.commit();
    }

    public static boolean checkLoginStatus(Activity context) {
        if (!isLogined(context)) {
            Intent intent = new Intent(context, RegLoginActivity.class);
            context.startActivityForResult(intent, 0);
            return false;
        }
        return true;
    }

    public static void overFirstTimeLaunched(Activity context) {
        Editor editor = getSharedPreferences(context).edit();
        editor.putBoolean("firsttime_launch", false);
        editor.commit();
    }

    public static boolean isFirstTimeLaunch(Activity context) {
        return getSharedPreferences(context).getBoolean("firsttime_launch", true);
    }
}
